package com.example.demo.service;

import com.example.demo.converter.ScheduleConverter;
import com.example.demo.dao.OutfitRepository;
import com.example.demo.dao.Schedule;
import com.example.demo.dao.ScheduleRepository;
import com.example.demo.dto.ScheduleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Set;

@Service

public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleRepository scheduleRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private OutfitService outfitService;
    @Override
    public Set<ScheduleDTO> getScheduleByUserId(long userId) {
        return ScheduleConverter.convertToScheduleDTOSet(scheduleRepository.findByUserId(userId));
    }

    @Override
    public Set<ScheduleDTO> getScheduleByUserIdAndDate(long userId, String date) {
        return ScheduleConverter.convertToScheduleDTOSet(scheduleRepository.findByUserIdAndDate(userId,date));
    }

    @Override
    public ScheduleDTO getScheduleByUserIdAndDateAndDateId(long userId, String  date, int dateId) {
        return ScheduleConverter.convertSchedule(scheduleRepository.findByUserIdAndDateAndDateId(userId,date,dateId).orElseThrow( ()-> new RuntimeException("Schedule not found")));
    }

    @Override
    public Long addScheduleByUserIdAndName(ScheduleDTO scheduleDTO, long userId,String outfitName) {
        scheduleDTO.setUserDTO(userService.getUserById(userId));
        scheduleDTO.setDateId(scheduleRepository.findMaxDateIdByDateAndUserId(scheduleDTO.getDate(),userId)+1);
        scheduleDTO.setOutfitDTO(outfitService.getOutfitByUserIdAndName(userId,outfitName));
         return scheduleRepository.save(ScheduleConverter.convertSchedule(scheduleDTO)).getId();
    }

    @Override
    public Long addAiSuggestionToScheduleById(long id, String aiSuggestion) {
        Schedule schedule=scheduleRepository.findById(id).orElseThrow(()->new RuntimeException( "Schedule not found"));
        schedule.setAiSuggestion(aiSuggestion);
        scheduleRepository.save(schedule);
        return schedule.getId();
    }
    @Override
    public void deleteScheduleByUserIdAndDate(long userId, String date) {
        scheduleRepository.deleteByUserIdAndDate(userId,date);
    }

    @Override
    public void deleteScheduleByUserIdAndDateAndDateId(long userId, String date, int dateId) {
        scheduleRepository.deleteByUserIdAndDateAndDateId(userId,date,dateId);
    }

    @Override
    public ScheduleDTO updateScheduleByUserIdAndDateAndDateId(long userId, String date, String content, String aiSuggestion,int dateId) {
        Schedule scheduleInDB = scheduleRepository.findByUserIdAndDateAndDateId(userId,date,dateId).orElseThrow( ()-> new RuntimeException("Schedule not found"));
        if (StringUtils.hasLength(content)&& !content.equals(scheduleInDB.getContent())){
            scheduleInDB.setContent(content);
        }
        if (StringUtils.hasLength(aiSuggestion)&& !aiSuggestion.equals(scheduleInDB.getAiSuggestion())){
            scheduleInDB.setAiSuggestion(aiSuggestion);
        }
        return ScheduleConverter.convertSchedule(scheduleRepository.save(scheduleInDB));
    }
}
